package com.carleasoft.mps.cms.business.message.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.carleasoft.mps.cms.business.message.bean.entity.MessageReceive;
import com.carleasoft.mps.cms.business.message.bean.po.MessageReceivePo;
import com.carleasoft.mps.cms.business.message.service.MessageReceiveService;
import com.carleasoft.mps.core.result.R;
import com.carleasoft.mps.core.user.ThreadUserContext;
import com.carleasoft.mps.core.user.UserCacheData;
import dm.jdbc.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 留言回复表 前端控制器
 * </p>
 *
 * @author fan
 * @since 2023-05-17
 */
@RestController
@RequestMapping("/messageReceive")
public class MessageReceiveController {
    @Autowired
    private MessageReceiveService messageReceiveService;
    /**
     * 添加留言回复
     */
    @PostMapping("/add")
    public R add(@RequestBody MessageReceive messageReceive){
        UserCacheData user = ThreadUserContext.getCurrentCacheUser();
        messageReceive.setReceiveCode(UUID.randomUUID().toString().replaceAll("-",""));
        messageReceive.setCreateTime(LocalDateTime.now());
        messageReceive.setCreatePersonCode(user.getUserCode());
        if(StringUtil.isEmpty(messageReceive.getCreatePersonName())){
            messageReceive.setCreatePersonName(user.getUserName());
        }
        return R.ok(messageReceiveService.save(messageReceive));
    }
    @PostMapping("/selMessage")
    public R selMessage(@RequestBody MessageReceive messageReceive){
        List<MessageReceive> res = messageReceiveService.list(new QueryWrapper<MessageReceive>().eq("MESSAGE_CODE", messageReceive.getMessageCode()));
        return R.ok(res);
    }
    /**
     * 分页查询留言回复
     */
    @PostMapping("/pageQuery")
    public R pageQuery(@RequestBody MessageReceivePo messageReceivePo){
        return R.ok(messageReceiveService.pageQuery(messageReceivePo));
    }
}

